#import "dfhgwhruebvhrheygeneralizeignore.h"
@interface dfhgwhruebvhrheygeneralizeignore ()
@property (nonatomic, assign) NSInteger itemCount;
@property (nonatomic, assign) CGFloat itemMargin;
@property (nonatomic, weak) UITextField *textField;
@property (nonatomic, weak) UIButton *maskView;
@property (nonatomic, strong) NSMutableArray<PPP_CursorLabel *> *labels;
@property (nonatomic, strong) NSMutableArray<UIView *> *lines;
@property (nonatomic, weak) PPP_CursorLabel *currentLabel;
@end
#define PPP_CodeViewLineColor						[UIColor whiteColor]
@implementation dfhgwhruebvhrheygeneralizeignore
- (instancetype)initWithCount:(NSInteger)count margin:(CGFloat)margin {
	if (self = [super init]) {
		self.itemCount = count;
		self.itemMargin = margin;
		[self configureTextField];
	}
	return self;
}
- (void)configureTextField {
	self.backgroundColor = [UIColor whiteColor];
	UITextField *textField = [[UITextField alloc] init];
	self.textField = textField;
	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
	textField.keyboardType = UIKeyboardTypeNumberPad;
	textField.tintColor = [UIColor clearColor];
	textField.textColor = [UIColor clearColor];
	[textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
	[self addSubview:textField];
	UIButton *maskView = [[UIButton alloc] init];
	self.maskView = maskView;
	maskView.backgroundColor = [UIColor clearColor];
	[maskView addTarget:self action:@selector(clickMaskView:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:maskView];
	for (NSInteger i = 0; i < self.itemCount; i++) {
		PPP_CursorLabel *label = [[PPP_CursorLabel alloc] init];
		label.textColor = [UIColor whiteColor];
		label.textAlignment = NSTextAlignmentCenter;
		label.font = [UIFont medium:20];
		[self.labels addObject:label];
		[self addSubview:label];
		UIView *line = [[UIView alloc] init];
		line.backgroundColor = PPP_CodeViewLineColor;
		[self.lines addObject:line];
		[self addSubview:line];
	}
}
- (void)setUnderLineColor:(UIColor *)underLineColor {
	for (UIView *line in self.lines) {
		line.backgroundColor = underLineColor;
	}
}
- (void)setCodeTextColor:(UIColor *)codeTextColor {
	for (PPP_CursorLabel *label in self.labels) {
		label.textColor = codeTextColor;
		label.cursorView.backgroundColor = codeTextColor;
	}
}
- (void)textFieldEditingChanged:(UITextField *)textField {
	if (textField.text.length > self.itemCount) {
		textField.text = [textField.text substringToIndex:self.itemCount];
	}
	for (NSInteger i = 0; i < self.labels.count; i++) {
		UILabel *label = self.labels[i];
		if (i < textField.text.length) {
			label.text = [textField.text substringWithRange:NSMakeRange(i, 1)];
		} else {
			label.text = nil;
		}
	}
	[self setupCursor];
	if (textField.text.length >= self.itemCount) {
		[textField resignFirstResponder];
		if (_delegate && [_delegate respondsToSelector:@selector(codeViewDidInputCompleted:)]) {
			[_delegate codeViewDidInputCompleted:self];
		}
	}
}
- (void)clickMaskView:(UIButton *)maskView {
	[self.textField becomeFirstResponder];
	[self setupCursor];
}
- (void)setupCursor {
	[self.currentLabel stopAnimating];
	NSInteger index = self.code.length;
	if (index < 0) {
		index = 0;
	}
	if (index >= self.labels.count) {
		index = self.labels.count - 1;
	}
	PPP_CursorLabel *label = self.labels[index];
	[label startAnimating];
	self.currentLabel = label;
}
#pragma mark - override
- (void)layoutSubviews {
	[super layoutSubviews];
	if (self.labels.count != self.itemCount) {
		return;
	}
	CGFloat temp = self.bounds.size.width - self.itemMargin * (self.itemCount - 1);
	CGFloat w = temp / (self.itemCount * 1.0);
	CGFloat x = 0;
	for (NSInteger i = 0; i < self.labels.count; i++) {
		x = i * (w + self.itemMargin);
		UILabel *label = self.labels[i];
		label.frame = CGRectMake(x, 0, w, self.bounds.size.height);
		UIView *line = self.lines[i];
		line.frame = CGRectMake(x, self.bounds.size.height - 1.0, w, 1.0);
	}
	self.textField.frame = self.bounds;
	self.maskView.frame = self.bounds;
}
- (BOOL)endEditing:(BOOL)force {
	[self.textField endEditing:force];
	[self.currentLabel stopAnimating];
	return [super endEditing:force];
}
#pragma mark - getter
- (NSString *)code {
	return self.textField.text;
}
- (NSMutableArray<PPP_CursorLabel *> *)labels {
	if (_labels) {
		return _labels;
	}
	_labels = [NSMutableArray arrayWithCapacity:self.itemCount];
	return _labels;
}
- (NSMutableArray<UIView *> *)lines {
	if (_lines) {
		return _lines;
	}
	_lines = [NSMutableArray arrayWithCapacity:self.itemCount];
	return _lines;
}
@end
@interface PPP_CursorLabel()
@end
@implementation PPP_CursorLabel
- (instancetype)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		[self setupView];
	}
	return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
	if (self = [super initWithCoder:aDecoder]) {
		[self setupView];
	}
	return self;
}
#pragma mark - setupView
- (void)setupView {
	UIView *cursorView = [[UIView alloc] init];
	self.cursorView = cursorView;
	cursorView.backgroundColor = self.textColor;
	cursorView.alpha = 0;
	[self addSubview:cursorView];
}
- (void)layoutSubviews {
	[super layoutSubviews];
	const CGFloat margin = 10.0;
	self.cursorView.bounds = CGRectMake(0, 0, 1.5, self.bounds.size.height - margin * 2.0);
	self.cursorView.center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0);
}
- (void)startAnimating {
	if (self.text.length > 0) {
		return;
	}
	CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
	animation.duration = 0.7;
	animation.repeatCount = MAXFLOAT;
	animation.removedOnCompletion = NO;
	animation.fromValue = @(0.02);
	animation.toValue = @(1.0);
	animation.fillMode = kCAFillModeForwards;
	animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
	[self.cursorView.layer addAnimation:animation forKey:@"opacity"];
}
- (void)stopAnimating {
	[self.cursorView.layer removeAnimationForKey:@"opacity"];
}
@end
